package com.example.book_recommend.service.impl;

import com.example.book_recommend.dao.RoleMapper;
import com.example.book_recommend.dao.RolePrivilegeMapper;
import com.example.book_recommend.entity.Role;
import com.example.book_recommend.entity.RolePrivilege;
import com.example.book_recommend.entity.RolePrivilegeKey;
import com.example.book_recommend.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RolePrivilegeMapper rolePrivilegeMapper;


    @Override
    public List<Role> getRolesByPage(Integer page, Integer size) {
        return roleMapper.getRolesByPage(page, size);
    }

    @Override
    public Role getRoleById(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public int addRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public int updateRoleById(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int deleteById(Integer roleId) {
        return roleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public int addPrivilege(RolePrivilege rolePrivilege) {
        RolePrivilege rolePrivilege1 = rolePrivilegeMapper.selectByPrimaryKey(new RolePrivilegeKey(rolePrivilege.getRoleId(), rolePrivilege.getPrivilegeId()));
        if(rolePrivilege1 != null){
            rolePrivilege1.setUpdated(rolePrivilege.getUpdated());
            rolePrivilegeMapper.updateByPrimaryKeySelective(rolePrivilege1);
            return 0;
        }
        return rolePrivilegeMapper.insert(rolePrivilege);
    }


}
